package com.zhxy.manager.controller;

import com.alibaba.excel.EasyExcel;
import com.zhxy.manager.entity.Computer;

import com.zhxy.manager.service.intf.ComputerService;

import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/computer/")
public class ComputerController {
    @Autowired
    private ComputerService service;
    //增
    @RequestMapping("save.do")
    public R save(Computer computer){
        return service.save(computer);
    }
    //查全部
    @RequestMapping("page.do")
    public List<Computer> query(){
        return service.queryAll();
    }
    //删除
    @PostMapping("del.do")
    public R del(int id){
        return service.del(id);
    }
    //修改
    @PostMapping("update.do")
    public R update(Computer computer){
        return service.update(computer);
    }
    //条件查询
    @GetMapping("pagewhere.do")
    public PageBean page(String name,int page,int limit){
        return service.queryPageWhere(name,page,limit);
    }
    //批量删除 JSON格式的数据请求
    @PostMapping("delbatch.do")
    public R del(@RequestBody int[] ids){
        return service.delBatch(ids);
    }

    //数据导出
    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Computer> list = service.queryAll();
        //2.设置响应消息头
        response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
        //3.写出内容
        EasyExcel.write(response.getOutputStream(), Computer.class).
                sheet("自动导出").doWrite(list);
    }

    //数据导入，Excel上传-解析-批量新增
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.saveBatch(file);
    }

    //统计
    @GetMapping("echarts.do")
    public R tj(){
        return service.queryTj();
    }
}
